package com.pur.zsms.cost.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.pur.zsms.common.dao.GenericDao;
import com.pur.zsms.common.dao.JpaTemplate;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.cost.dao.ICostDao;
import com.pur.zsms.cost.po.CostPo;
import com.pur.zsms.cost.po.CostSubPo;
import com.pur.zsms.cost.vo.CostVo;
import com.pur.zsms.financing.vo.FinancingVo;
import com.pur.zsms.foodmanage.food.po.FoodPo;
import com.pur.zsms.matermanage.mater.po.MaterLevelPo;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.systemsetting.datadictionary.dao.IDataDictionaryDao;
import com.pur.zsms.systemsetting.datadictionary.po.Code;
import com.pur.zsms.systemsetting.datadictionary.po.CodeType;
import com.pur.zsms.systemsetting.datadictionary.vo.CodeVo;
import com.pur.zsms.timecontrol.dao.ITimeControlDao;
import com.pur.zsms.timecontrol.po.TimeControlPo;
import com.pur.zsms.turnover.vo.TurnoverVo;

@Repository("costDao")
public class CostDaoImpl extends GenericDao implements ICostDao {
	public List<CostPo> getCostPo(CostVo tCostVo) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();
		ql.append(" select o ");
		ql.append(" from CostPo o where 1=1 ");
		if (tCostVo.getCustId() != 0) {
			ql.append(" and o.custId ='" + tCostVo.getCustId() + "'");
		}
		if (tCostVo.getCostDate() != null && !"".equals(tCostVo.getCostDate())) {
			ql.append(" and o.costDate ='" + tCostVo.getCostDate() + "'");
		}
		if (tCostVo.getCostType() != 0) {
			ql.append(" and o.costType ='" + tCostVo.getCostType() + "'");
		}
		if (tCostVo.getCostState() != 0) {
			ql.append(" and o.costState ='" + tCostVo.getCostState() + "'");
		}
		if (tCostVo.getCostMonth() != null && !"".equals(tCostVo.getCostMonth())) {
			ql.append(" and o.costDate like '" + tCostVo.getCostMonth() + "%'");
		}

		List<CostPo> list = template.find(ql.toString(), null);
		return list;
	}

	public void addCostPo(CostPo c) throws GenericException {
		this.persist(c);
	}

	public void updateCostPo(CostPo c) throws GenericException {
		this.merge(c);
	}

	public void addCostSubPo(CostSubPo c) throws GenericException {
		this.persist(c);
	}

	public List<Object> getMaxAddId(FinancingVo tFinancingVo) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT MAX(ys.cost_sub_id) FROM c_cost_sub ys,c_cost y1  WHERE ys.cost_id = y1.cost_id AND y1.cust_id = '"
				+ tFinancingVo.getCustId() + "' AND y1.cost_date='" + tFinancingVo.getDate() + "'");
		List list = this.entityManager.createNativeQuery(sql.toString()).getResultList();
		return list;
	}

	public List<CostSubPo> getFileName(long costId, long costSubId) {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();
		ql.append(" select o ");
		ql.append(" from CostSubPo o where 1=1 ");
		ql.append(" and o.costId ='" + costId + "'");
		ql.append(" and o.costSubId ='" + costSubId + "'");
		List<CostSubPo> list = template.find(ql.toString(), null);
		return list;
	}

	public PaginationSupport listCostSubList(PageController pageController, CostVo costVo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT ys.cost_id,ys.cost_sub_id,ys.cost_type,ys.cost_value,ys.cost_remark,ys.file_name FROM c_cost_sub ys, c_cost y1 WHERE ys.cost_id = y1.cost_id AND y1.cust_id = '"
				+ costVo.getCustId() + "' AND y1.cost_date = '" + costVo.getCostDate() + "' ");
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public PaginationSupport costDetailForManager(PageController pageController, CostVo costVo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT c1.cost_type, SUM(c1.cost_value) FROM c_cost_sub c1, c_cost c2 WHERE c2.cost_id = c1.cost_id AND c2.cost_date>= '"
				+ costVo.getCostDateStart() + "' AND c2.cost_date<= '" + costVo.getCostDateEnd() + "' AND c2.cust_id='" + costVo.getCustId()
				+ "' GROUP BY c1.cost_type ");
		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public void deleteCost(FinancingVo c) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();
		ql.append(" delete CostPo o where o.costId=" + c.getCostId());
		template.executeUpdate(ql.toString());

		ql = new StringBuffer("");
		ql.append(" delete CostSubPo o where o.costId=" + c.getCostId());
		template.executeUpdate(ql.toString());
	}

}
